{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入某年某月某日，判断这一天是这一年的第几天？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018年6月5日是这一年的第156天\n"
     ]
    }
   ],
   "source": [
    "def is_leap_year(year):\n",
    "    \"\"\"判断是否是闰年\"\"\"\n",
    "    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):\n",
    "        return True\n",
    "    return False\n",
    "\n",
    "def day_of_year(year, month, day):\n",
    "    \"\"\"计算这一天是这一年的第几天\"\"\"\n",
    "    # 每个月的天数（平年）\n",
    "    days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "    \n",
    "    # 如果是闰年，二月有29天\n",
    "    if is_leap_year(year):\n",
    "        days_in_month[1] = 29\n",
    "    \n",
    "    # 计算从年初到当前月份之前的所有天数\n",
    "    total_days = sum(days_in_month[:month-1]) + day\n",
    "    \n",
    "    return total_days\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    # 输入年份、月份和日期\n",
    "    year = int(input(\"请输入年份: \"))\n",
    "    month = int(input(\"请输入月份: \"))\n",
    "    day = int(input(\"请输入日期: \"))\n",
    "    \n",
    "    # 计算并输出结果\n",
    "    result = day_of_year(year, month, day)\n",
    "    print(f\"{year}年{month}月{day}日是这一年的第{result}天\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "判断1000中有多少个素数，并输出所有素数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内共有168个素数。\n",
      "这些素数是：\n",
      "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "def is_prime(n):\n",
    "    \"\"\"判断一个数是否为素数\"\"\"\n",
    "    if n <= 1:\n",
    "        return False\n",
    "    for i in range(2, int(n**0.5) + 1):\n",
    "        if n % i == 0:\n",
    "            return False\n",
    "    return True\n",
    "\n",
    "def find_primes_up_to(limit):\n",
    "    \"\"\"找到指定范围内的所有素数\"\"\"\n",
    "    primes = []\n",
    "    for num in range(2, limit + 1):\n",
    "        if is_prime(num):\n",
    "            primes.append(num)\n",
    "    return primes\n",
    "\n",
    "# 设置范围上限\n",
    "limit = 1000\n",
    "\n",
    "# 找到所有素数\n",
    "primes = find_primes_up_to(limit)\n",
    "\n",
    "# 输出结果\n",
    "print(f\"1000以内共有{len(primes)}个素数。\")\n",
    "print(\"这些素数是：\")\n",
    "print(primes)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。（要求有键盘输入）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你的成绩等级是: B\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入的成绩\n",
    "score = int(input(\"请输入你的学习成绩: \"))\n",
    "\n",
    "# 使用条件运算符的嵌套来判断成绩等级\n",
    "grade = 'A' if score >= 90 else ('B' if score >= 60 else 'C')\n",
    "\n",
    "# 输出成绩等级\n",
    "print(f\"你的成绩等级是: {grade}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "英文字母个数: 11\n",
      "空格个数: 3\n",
      "数字个数: 4\n",
      "其它字符个数: 3\n"
     ]
    }
   ],
   "source": [
    "def count_characters(input_string):\n",
    "    # 初始化计数器\n",
    "    letters = 0\n",
    "    spaces = 0\n",
    "    digits = 0\n",
    "    others = 0\n",
    "    \n",
    "    # 遍历字符串中的每个字符\n",
    "    for char in input_string:\n",
    "        if char.isalpha():\n",
    "            letters += 1\n",
    "        elif char.isspace():\n",
    "            spaces += 1\n",
    "        elif char.isdigit():\n",
    "            digits += 1\n",
    "        else:\n",
    "            others += 1\n",
    "    \n",
    "    return letters, spaces, digits, others\n",
    "\n",
    "# 获取用户输入的字符串\n",
    "input_string = input(\"请输入一行字符串: \")\n",
    "\n",
    "# 调用函数并获取结果\n",
    "letters, spaces, digits, others = count_characters(input_string)\n",
    "\n",
    "# 输出结果\n",
    "print(f\"英文字母个数: {letters}\")\n",
    "print(f\"空格个数: {spaces}\")\n",
    "print(f\"数字个数: {digits}\")\n",
    "print(f\"其它字符个数: {others}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一个数如果恰好等于它的因子之和，这个数就称为“完数”。例如6=1＋2＋3.编程，找出1000以内的所有完数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内的完数有： [6, 28, 496]\n"
     ]
    }
   ],
   "source": [
    "def find_factors(n):\n",
    "    \"\"\"返回n的所有真因子\"\"\"\n",
    "    factors = []\n",
    "    for i in range(1, n):\n",
    "        if n % i == 0:\n",
    "            factors.append(i)\n",
    "    return factors\n",
    "\n",
    "def is_perfect_number(n):\n",
    "    \"\"\"判断一个数是否为完数\"\"\"\n",
    "    return sum(find_factors(n)) == n\n",
    "\n",
    "def find_perfect_numbers(limit):\n",
    "    \"\"\"找出指定范围内的所有完数\"\"\"\n",
    "    perfect_numbers = []\n",
    "    for num in range(2, limit + 1):\n",
    "        if is_perfect_number(num):\n",
    "            perfect_numbers.append(num)\n",
    "    return perfect_numbers\n",
    "\n",
    "# 找出1000以内的所有完数\n",
    "perfect_numbers = find_perfect_numbers(1000)\n",
    "print(\"1000以内的完数有：\", perfect_numbers)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入任意数N，求1+2!+3!+...+N!的值（注意实数范围）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 + 2! + 3! + ... + 9! 的值是: 409113.0\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "def factorial(n):\n",
    "    \"\"\"计算n的阶乘\"\"\"\n",
    "    if n == 0 or n == 1:\n",
    "        return 1\n",
    "    else:\n",
    "        return math.gamma(n + 1)  # 使用伽玛函数来计算阶乘\n",
    "\n",
    "def sum_of_factorials(N):\n",
    "    \"\"\"计算1 + 2! + 3! + ... + N!的值\"\"\"\n",
    "    total_sum = 0\n",
    "    for i in range(1, int(math.floor(N)) + 1):\n",
    "        total_sum += factorial(i)\n",
    "    return total_sum\n",
    "\n",
    "# 输入任意实数N\n",
    "N = float(input(\"请输入一个实数N: \"))\n",
    "result = sum_of_factorials(N)\n",
    "print(f\"1 + 2! + 3! + ... + {int(math.floor(N))}! 的值是: {result}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入任意奇数N，按如下要求打印出图案（菱形）\n",
    "    *\n",
    "  ***\n",
    " *****\n",
    "*******（N控制行数和本行的星数）\n",
    " *****\n",
    "  ***\n",
    "   *\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    *    \n",
      "   ***   \n",
      "  *****  \n",
      " ******* \n",
      "*********\n",
      " ******* \n",
      "  *****  \n",
      "   ***   \n",
      "    *    \n"
     ]
    }
   ],
   "source": [
    "def print_diamond(N):\n",
    "    if N % 2 == 0:\n",
    "        print(\"请输入一个奇数\")\n",
    "        return\n",
    "    \n",
    "    # 上半部分（包括中间行）\n",
    "    for i in range(1, N + 1, 2):\n",
    "        spaces = (N - i) // 2\n",
    "        print(' ' * spaces + '*' * i + ' ' * spaces)\n",
    "    \n",
    "    # 下半部分\n",
    "    for i in range(N - 2, 0, -2):\n",
    "        spaces = (N - i) // 2\n",
    "        print(' ' * spaces + '*' * i + ' ' * spaces)\n",
    "\n",
    "# 输入任意奇数N\n",
    "N = int(input(\"请输入一个奇数N: \"))\n",
    "print_diamond(N)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对10个数进行排序。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序后的数组: [8, 25, 33, 39, 45, 52, 74, 75, 81, 367]\n"
     ]
    }
   ],
   "source": [
    "def bubble_sort(arr):\n",
    "    n = len(arr)\n",
    "    for i in range(n):\n",
    "        for j in range(0, n-i-1):\n",
    "            if arr[j] > arr[j+1]:\n",
    "                arr[j], arr[j+1] = arr[j+1], arr[j]\n",
    "    return arr\n",
    "\n",
    "# 示例\n",
    "numbers = [45,52, 75, 367, 25, 74, 33, 8, 81, 39]\n",
    "sorted_numbers = bubble_sort(numbers)\n",
    "print(\"排序后的数组:\", sorted_numbers)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "插入后的数组是:\n",
      "1\n",
      "3\n",
      "4.0\n",
      "5\n",
      "7\n",
      "9\n",
      "11\n",
      "13\n",
      "15\n",
      "17\n",
      "19\n"
     ]
    }
   ],
   "source": [
    "def insert_into_sorted_array(arr, num):\n",
    "    # 使用二分查找法找到插入位置\n",
    "    left, right = 0, len(arr) - 1\n",
    "    while left <= right:\n",
    "        mid = (left + right) // 2\n",
    "        if arr[mid] < num:\n",
    "            left = mid + 1\n",
    "        else:\n",
    "            right = mid - 1\n",
    "    # 在找到的位置插入数字\n",
    "    arr.insert(left, num)\n",
    "    return arr\n",
    "\n",
    "# 假设这是已经排好序的数组\n",
    "sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n",
    "\n",
    "# 从键盘输入一个数\n",
    "num = float(input(\"请输入一个数: \"))\n",
    "\n",
    "# 将数插入到数组中\n",
    "new_array = insert_into_sorted_array(sorted_array, num)\n",
    "\n",
    "# 输出新的数组\n",
    "print(\"插入后的数组是:\")\n",
    "for element in new_array:\n",
    "    print(element)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最后留下的是原来第29号的那位\n"
     ]
    }
   ],
   "source": [
    "def josephus(n, k):\n",
    "    if n == 1:\n",
    "        return 0\n",
    "    else:\n",
    "        return (josephus(n - 1, k) + k) % n\n",
    "\n",
    "# 输入总人数n和报数的上限k\n",
    "n = int(input(\"请输入总人数n: \"))\n",
    "k = 3  # 从1到3报数，即k=3\n",
    "\n",
    "# 计算最后留下的是原来第几号的那位\n",
    "last_person = josephus(n, k) + 1  # 加1是因为返回的是索引，而题目中编号是从1开始的\n",
    "print(f\"最后留下的是原来第{last_person}号的那位\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
